package com.yanqu.road.dao.impl.drugstore;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.drugstore.DrugHardCaseRecordData;
import com.yanqu.road.entity.drugstore.DrugStoreHardCaseDispenseResultInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description
 * @Author cwq
 * @Data 2022/4/18 17:48
 */
public class DrugHardCaseRecordDataDaoImpl extends TempDataObjectDao<DrugHardCaseRecordData> {

    public static String getDrugHardCaseRecordDataSql = "select * from `t_u_drug_store_hardcase_record` where `userId`=?";
    public Map<Integer, List<DrugHardCaseRecordData>> getDrugHardCaseRecordDataMap(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, List<DrugHardCaseRecordData>> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getDrugHardCaseRecordDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                DrugHardCaseRecordData recordData = parse(rs);
                List<DrugHardCaseRecordData> list = map.get(recordData.getCaseId());
                if(list == null){
                    list = new ArrayList<>();
                    map.put(recordData.getCaseId(), list);
                }
                list.add(recordData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateDrugHardCaseRecordDataSql = "update `t_u_drug_store_hardcase_record` set initHealth=?,finalHealth=?,score=?,status=?,crudeDrugList=?,drugIntroductionInfo=?,orderInfo=?,time=? where userId=? and caseId=?";
    @Override
    protected boolean updateData(DrugHardCaseRecordData drugHardCaseRecordData) {
        drugHardCaseRecordData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, drugHardCaseRecordData.getInitHealth()));
        para.put(i++, new DbParameter(Types.INTEGER, drugHardCaseRecordData.getFinalHealth()));
        para.put(i++, new DbParameter(Types.INTEGER, drugHardCaseRecordData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, drugHardCaseRecordData.getStatus()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(drugHardCaseRecordData.getCrudeDrugList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(drugHardCaseRecordData.getDrugIntroductionInfo())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(drugHardCaseRecordData.getOrderInfo())));
        para.put(i++, new DbParameter(Types.BIGINT, drugHardCaseRecordData.getTime()));
        para.put(i++, new DbParameter(Types.BIGINT, drugHardCaseRecordData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, drugHardCaseRecordData.getCaseId()));
        if (getDb().executeNoneQuery(updateDrugHardCaseRecordDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertDrugHardCaseRecordDataSql = "insert into `t_u_drug_store_hardcase_record`(userId,caseId,initHealth,finalHealth,score,status,crudeDrugList,drugIntroductionInfo,orderInfo,time) values (?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(DrugHardCaseRecordData drugHardCaseRecordData) {
        drugHardCaseRecordData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, drugHardCaseRecordData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, drugHardCaseRecordData.getCaseId()));
        para.put(i++, new DbParameter(Types.INTEGER, drugHardCaseRecordData.getInitHealth()));
        para.put(i++, new DbParameter(Types.INTEGER, drugHardCaseRecordData.getFinalHealth()));
        para.put(i++, new DbParameter(Types.INTEGER, drugHardCaseRecordData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, drugHardCaseRecordData.getStatus()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(drugHardCaseRecordData.getCrudeDrugList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(drugHardCaseRecordData.getDrugIntroductionInfo())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(drugHardCaseRecordData.getOrderInfo())));
        para.put(i++, new DbParameter(Types.BIGINT, drugHardCaseRecordData.getTime()));
        if (getDb().executeNoneQuery(insertDrugHardCaseRecordDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected DrugHardCaseRecordData parseData(ResultSet rs) throws SQLException {
        DrugHardCaseRecordData drugHardCaseRecordData = new DrugHardCaseRecordData();
        drugHardCaseRecordData.setUserId(rs.getLong("userId"));
        drugHardCaseRecordData.setCaseId(rs.getInt("caseId"));
        drugHardCaseRecordData.setInitHealth(rs.getInt("initHealth"));
        drugHardCaseRecordData.setFinalHealth(rs.getInt("finalHealth"));
        drugHardCaseRecordData.setScore(rs.getInt("score"));
        drugHardCaseRecordData.setStatus(rs.getInt("status"));
        drugHardCaseRecordData.setCrudeDrugList(JSON.parseObject(rs.getString("crudeDrugList"), new TypeReference<java.util.List<com.yanqu.road.entity.drugstore.DrugStoreHardCaseDispenseResultInfo>>() {}));
        drugHardCaseRecordData.setDrugIntroductionInfo(JSON.parseObject(rs.getString("drugIntroductionInfo"), DrugStoreHardCaseDispenseResultInfo.class));
        drugHardCaseRecordData.setOrderInfo(JSON.parseObject(rs.getString("orderInfo"), DrugStoreHardCaseDispenseResultInfo.class));
        drugHardCaseRecordData.setTime(rs.getLong("time"));

        return drugHardCaseRecordData;
    }
}
